// 退货原因
<template>
   <div class="search">
     <Row>
        <Card>
          <Row class="operation">
            <Form inline :label-width="100" ref="searchForm" :model="searchForm" label-position="right">
              <Button @click="getDataList()" icon="md-refresh">刷新</Button>
              <Button @click="add" type="primary" icon="md-add">添加</Button>
            </Form>
          </Row>
          <!-- <Row>
            <Alert show-icon>
              <a class="select-clear" @click="clearSelectAll">清空</a>
            </Alert> 
          </Row> -->
          <Row>
            <Table :loading="loading" border :columns="columns" :data="data" ref="table" sortable="custom" @on-sort-change="changeSort"></Table>
          </Row>
          <Row type="flex" justify="end" class="page">
            <Page :current="searchForm.pageNumber" :total="total" :page-size="searchForm.pageSize" @on-change="changePage" @on-page-size-change="changePageSize" :page-size-opts="[10,20,50]" size="small" show-total show-elevator show-sizer></Page>
          </Row>
        </Card>
        <Modal :title="modalTitle" v-model="modalVisible" :mask-closable='false' :width="700">
          <Form ref="formadd" :model="formadd" :label-width="100" :rules="formValidate" >
            <FormItem label="退货原因" prop="reason">
              <Input v-model="formadd.reason" autocomplete="off"/>
            </FormItem>
          </Form>
          <div slot="footer">
            <Button type="text" @click="modalVisible=false">取消</Button>
            <Button type="primary" :loading="submitLoading" @click="handleSubmit">提交</Button>
          </div>
        </Modal>
     </Row>
   </div> 
</template>

<script>
export default {
  data(){
    return{ 
      total:0,
      loading: true, // 表单加载状态
      data:[],//表单数据
      submitLoading: false, // 添加或编辑提交状态
      modalTitle:'',
      modalType: 0,
      modalVisible:false,  
      searchForm:{
        pageSize:10,
        pageNumber:1
      },
      formadd:{
        id: "",
        reason: ""
      },
      formValidate: {
        reason: [{ required: true, message: "退货原因不能为空", trigger: "blur" }]
      },
      columns: [
        // 表头
        {
          type: "index",
          width: 60,
          align: "center"
        },
        {
          title: "退货原因",
          key: "reason",
          minWidth: 120,
          sortable: false,
          
        },
        {
          title: "创建时间",
          key: "createTime",
          minWidth: 120,
          sortable: false,
        },
        {
          title: "操作",
          key: "action",
          align: "center",
          minWidth: 200,
          render: (h, params) => {
            return h("div", [
              h(
                "Button",
                {
                  props: {
                    type: "primary",
                    size: "small",
                    icon: "ios-create-outline"
                  },
                  style: {
                    marginRight: "5px"
                  },
                  on: {
                    click: () => {
                      this.edit(params.row);
                    }
                  }
                },
                "编辑"
              ),
              h(
                "Button",
                {
                  props: {
                    type: "error",
                    size: "small"
                  },
                  style: {
                    marginRight: "5px"
                  },
                  on: {
                    click: () => {
                      this.remove(params.row);
                    }
                  }
                },
                "删除"
              )
            ]);
          }
        }
      ],
    }
  },
  methods:{
    init(){
      this.getDataList()
    },
    getDataList(){
      this.postRequest("/orderReturnReason/backstageOrderReturnReasonList",this.searchForm).then(res =>{
        this.loading = false
        if(res.success){
          console.log(res)
          this.data = res.result.data;
        }
      })
    },
    //添加退货原因
    add(){
      this.modalTitle = "添加"
      this.modalVisible = true;
      this.modalType = 0;
    },
    //编辑
    edit(v){
        this.modalTitle = "编辑"
        this.formadd.id = v.id;
        this.formadd.reason  = v.reason;
        this.modalVisible = true;
        this.modalType = 1;
    },
    //删除
    remove(v){
        console.log(v)
        this.$Modal.confirm({
            title: "确认删除",
            content: "您确认要删除所选的数据?",
            onOk: () => {
                let params = {
                    ids:v.id
                }
                this.postRequest("/orderReturnReason/remOveorderReturnReason",params).then(res =>{
                if(res.success){
                    this.$Message.success("删除成功")
                    this.getDataList()
                }
                })
            }
        })
      },
        //提交
        handleSubmit(){
          this.submitLoading = true;
          this.$refs.formadd.validate(valid => {
            if (valid) {
              if (this.modalType == 0) {
                this.postRequest("/orderReturnReason/insertOrderUpdateorderReturnReason",this.formadd).then(res =>{
                  console.log(res)
                  this.submitLoading = false;
                  if(res.success){
                    this.$Message.success("提交成功");
                    this.getDataList();
                    this.$refs.formadd.resetFields()
                    this.modalVisible = false
                  } else {
                    this.$Message.success(res.message);
                    this.getDataList();
                    this.$refs.formadd.resetFields()
                    this.modalVisible = false
                  }
                })
              } else {
                this.postRequest("/orderReturnReason/insertOrderUpdateorderReturnReason",this.formadd).then(res =>{
                  console.log(res)
                  this.submitLoading = false;
                  if(res.success){
                    this.$Message.success("修改成功");
                    this.getDataList();
                    this.$refs.formadd.resetFields()
                    this.modalVisible = false
                  } else {
                    this.$Message.success(res.message);
                    this.getDataList();
                    this.$refs.formadd.resetFields()
                    this.modalVisible = false
                  }
                })
              }
            }
          })
        },
    changePage(v) {
      this.searchForm.pageNumber = v;
      this.getDataList();
      // this.clearSelectAll();
    },
    changePageSize(v) {
      this.searchForm.pageSize = v;
      this.getDataList();
    },
     handleChangecity(v){
           console.log(v)
          this.formadd.areaId = v[v.length-1]
          console.log(this.formadd.areaId)
        },
        handleChange(v){
          console.log(v)
          this.formadd.classId = v[v.length-1]
          console.log(this.formadd.classId)
        },
      getTree(tree = []) {
        let arr = [];
        if (!!tree && tree.length !== 0) {
            tree.forEach(item => {
                let obj = {};
                obj.value = item.id;
                obj.label= item.title
                obj.children = this.getTree(item.backAreaVOS); // 递归调用
                arr.push(obj);
            });
        }
        return arr;
    },
    },
    mounted(){
        this.init()
    }
}
</script>

<style lang="less">
.search {
    .operation {
        margin-bottom: 2vh;
    }
    .select-count {
        font-size: 13px;
        font-weight: 600;  
        color: #40a9ff;
    }
    .select-clear {
        margin-left: 10px;
    }
    .page {
        margin-top: 2vh;
    }
    .drop-down {
        font-size: 13px;
        margin-left: 5px;
    }
}
</style>